<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\helpers\Url;
use kartik\daterange\DateRangePicker;
/* @var $this yii\web\View */
/* @var $searchModel app\models\NewsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', '资讯列表');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="news-index box box-primary">
    <?php Pjax::begin(['id' => 'pjax', 'options' => ['class' => 'pjax'], 'timeout' => 300000, 'linkSelector' => false, 'scrollTo' => false]);?>
    <div class="box-header with-border">
        <!-- <?= Html::a(Yii::t('app', '新建项目'), ['create'], ['class' => 'btn btn-success btn-flat']) ?> -->
        <form role="form" id='batch-upload' action='<?=Url::to(['batch-upload'])?>' enctype="multipart/form-data">
              <input type="hidden" name="_csrf" value="<?= \Yii::$app->request->csrfToken ?>" />
              <div class="box-body">
                <div class="form-group">
                  <label for="exampleInputFile">File input</label>
                  <input type="file" id="exampleInputFile" name="file">

                  <p class="help-block" style="color: red">相同的表格内容切勿重复导入！慎用！</p>
                </div>
              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" class="btn btn-primary">批量导入</button>
              </div>
            </form>
    </div>
    <div class="box-body table-responsive no-padding">

        <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'layout' => "{items}\n{summary}\n{pager}",
            'emptyText' =>'没有符合条件的数据!',
            'columns' => [
                // ['class' => 'yii\grid\SerialColumn'],

                'id',
                'title',
                [   
                    'attribute' => 'username',
                    'label'=>'文章作者',
                ],
                'email:email',
                [   
                    'attribute' => 'category',
                    'format' => 'html',
                    'value'=>function($model) use ($newsCategory){
                        return $newsCategory->getCategoryName($model['category']);
                    },
                    'filter' => $newsCategory->getDropDownList(),     //此处我们可以将筛选项组合成key-value形式
                ],
                [    
                    'options' => ['width' => '110px'],
                    'attribute' => 'status',
                    'format' => 'html',
                    'value'=>function($model) use ($searchModel){
                        return $searchModel::getCheckStatus($model['status']);
                    },
                    'filter' => $searchModel::getCheckStatusKeyPair(),     //此处我们可以将筛选项组合成key-value形式
                ],
                'opt_note:ntext',
                //创建时间
                [
                    'attribute'          => 'created_time',
                    'format' => ['date', "php:Y-m-d H:i:s"],
                    'headerOptions' => ['width' => '12%'],
                    'filter' => DateRangePicker::widget([
                        'name' => 'NewsSearch[created_time]',
                        'language'=>'zh-cn', 
                        'value' => Yii::$app->request->get('NewsSearch')['created_time'],
                        'convertFormat' => true,
                        'pluginOptions' => [
                            'timePicker'=>true,
                            'timePickerIncrement'=>15,
                            'locale'=>['format'=>'Y-m-d H:i:s'],
                            'ranges' => [
                                Yii::t('app', "Today") => ["moment().startOf('day')", "moment()"],
                                Yii::t('app', "Yesterday") => ["moment().startOf('day').subtract(1,'days')", "moment().endOf('day').subtract(1,'days')"],
                                Yii::t('app', "Last {n} Days", ['n' => 7]) => ["moment().startOf('day').subtract(6, 'days')", "moment()"],
                                Yii::t('app', "Last {n} Days", ['n' => 30]) => ["moment().startOf('day').subtract(29, 'days')", "moment()"],
                                Yii::t('app', "This Month") => ["moment().startOf('month')", "moment().endOf('month')"],
                                Yii::t('app', "Last Month") => ["moment().subtract(1, 'month').startOf('month')", "moment().subtract(1, 'month').endOf('month')"],
                            ],
                        ]
                    ]),

                    
                ],
                //更新时间
                // [
                //     'attribute'          => 'updated_time',
                //     'format' => ['date', "php:Y-m-d H:i:s"],
                //     'headerOptions' => ['width' => '12%'],
                //     'filter' => DateRangePicker::widget([
                //         'name' => 'NewsSearch[updated_time]',
                //         'language'=>'zh-cn', 
                //         'value' => Yii::$app->request->get('NewsSearch')['updated_time'],
                //         'convertFormat' => true,
                //         'pluginOptions' => [
                //             'timePicker'=>true,
                //             'timePickerIncrement'=>15,
                //             'locale'=>['format'=>'Y-m-d H:i:s'],
                //             'ranges' => [
                //                 Yii::t('app', "Today") => ["moment().startOf('day')", "moment()"],
                //                 Yii::t('app', "Yesterday") => ["moment().startOf('day').subtract(1,'days')", "moment().endOf('day').subtract(1,'days')"],
                //                 Yii::t('app', "Last {n} Days", ['n' => 7]) => ["moment().startOf('day').subtract(6, 'days')", "moment()"],
                //                 Yii::t('app', "Last {n} Days", ['n' => 30]) => ["moment().startOf('day').subtract(29, 'days')", "moment()"],
                //                 Yii::t('app', "This Month") => ["moment().startOf('month')", "moment().endOf('month')"],
                //                 Yii::t('app', "Last Month") => ["moment().subtract(1, 'month').startOf('month')", "moment().subtract(1, 'month').endOf('month')"],
                //             ],
                //         ]
                //     ]),
                // ],

                //操作按钮
                [
                    'options' => ['width' => '135px'],
                    'filter'  => Html::a(Yii::t('app', '重置'), Url::to(['']), ['class' => 'btn btn-info', 'style' => 'color: #ffffff;']),
                    'format'  => 'raw',
                    'label'   => Yii::t('app', '操作'),
                    'value'   => function ($model) {
                            $actionDom = Html::a(
                                Html::tag('span', '',['class' => 'glyphicon glyphicon-eye-open']),
                                Url::to(['view', 'id' => $model['id']]),
                                [
                                    'title' => 'View',
                                    'aria-label'=>"View",
                                    'data'  => [
                                        'pjax' => 0,     
                                    ],
                                ]
                            );
                        
                            // $actionDom .= '  ';
                            // $actionDom .= Html::a(
                            //     Html::tag('span', '',['class' => 'glyphicon glyphicon-pencil']),
                            //     Url::to(['update', 'id' => $model['id']]),
                            //     [
                            //         'title' => 'Update',
                            //         'aria-label'=>"Update",
                            //         'data'  => [
                            //             'pjax' => 0,
                            //         ],
                            //     ]
                            // );

                            // $actionDom .= '  ';
                            // $actionDom .= Html::a(
                            //     Html::tag('span', '',['class' => 'glyphicon glyphicon-trash']),
                            //     Url::to(['delete', 'id' => $model['id']]),
                            //     [
                            //         'title' => 'Delete',
                            //         'aria-label'=>"Delete",
                            //         'data'  => [
                            //             'confirm' => Yii::t('app', '确定删除吗？'),
                            //             'pjax' => 0,
                            //             'method' => 'post'
                            //         ],
                            //     ]
                            // );
                            return $actionDom;
                    },
                ],
            ],
        ]); ?>
    </div>
    <?php Pjax::end(); ?>
</div>

<!-- js代码块 引入日期过滤插件-->
<?php $this->beginBlock('script') ?> 
    
$("#batch-upload").submit(function(event) {
var form = new FormData(this);

trans_box_index = layer.msg('批量导入中...', {icon: 16, time: 0, shadeClose: true, shade: [0.7, '#000']});
$.ajax({
    url: $(this).attr('action'),
    type: 'POST',
    dataType: 'json',
    contentType: false, 
    processData: false,
    data: form,
    success: function(data){
       if (data.code != 0) {
           layer.alert(data.message, {title: '提示', btn: "", shadeClose: true, shade: [0.7, '#000'], offset: "auto", time: 2000});
       }else {
            location.reload();
       }
    }
});
return false;
});

<?php $this->endBlock() ?>
<?php //按需注入js, 放在</body>前面
$this->registerJs($this->blocks['script'], \yii\web\View::POS_END);
 ?>


